package com.cathychen.springactiondemo.web.model;

import lombok.Data;
import org.hibernate.validator.constraints.CreditCardNumber;

import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenjing
 * @date 2020/8/26
 */
@Data
public class Order {
    private Long id;
    private Date placedAt;

    @NotBlank
    private String deliveryName;

    @CreditCardNumber(message = "请正确输入信用卡号")
    private String ccNumber;

    @Pattern(regexp = "^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$",message = "格式应为MM/YY")
    private String ccExpiration;

    @Digits(integer = 3,fraction = 0,message = "CVV不合法")
    private String ccCVV;

    @PrePersist
    void placedAt() {
        this.placedAt = new Date();
    }

    @ManyToMany(targetEntity=Taco.class)
    private List<Taco> tacos = new ArrayList<>();

    public void addDesign(Taco design) {
        this.tacos.add(design);
    }
}
